<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <!-- 引入 jQuery -->
    <script src="/js/jquery-3.6.4.js"></script>
    <style>
        body {
            font-family: '微软雅黑', Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
        .login-container {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        .login-form {
            margin-bottom: 20px;
        }
        .login-input {
            padding: 10px;
            margin: 10px 0;
            width: 80%;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .login-button {
            padding: 10px 20px;
            margin: 10px 5px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            color: white;
            background-color: #007BFF;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .login-button:hover {
            background-color: #0056b3;
        }
        .register-button {
            padding: 10px 20px;
            margin: 10px 5px;
            font-size: 16px;
            border: 1px solid #007BFF;
            border-radius: 5px;
            color: #007BFF;
            background-color: white;
            cursor: pointer;
            transition: color 0.3s, border-color 0.3s;
        }
        .register-button:hover {
            color: white;
            background-color: #007BFF;
            border-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <form id="loginForm" class="login-form" action="/api/users/login" method="post">
        <div>
            <label for="username">用户名:</label>
            <input type="text" id="username" class="login-input" name="username" required>
        </div>
        <div>
            <label for="password">密码:</label>
            <input type="password" id="password" class="login-input" name="password" required>
        </div>
        <div>
            <button type="submit" class="login-button">登录</button>
        </div>
    </form>
    <button class="register-button" onclick="goToUserRegister();">注册</button>

    <script>
        // JavaScript函数，用于跳转到用户注册页面
        function goToUserRegister() {
            window.location.href = '/userregister';
        }

        // jQuery 用于处理登录表单的提交
        $(document).ready(function() {
            $('#loginForm').submit(function(event) {
                event.preventDefault();
                var username = $('#username').val();
                var password = $('#password').val();
                $.ajax({
                    url: '/api/users/login',
                    type: 'POST',
                    data: {
                        username: username,
                        password: password
                    },
                    success: function(response) {
                        var userId = response.id; // 根据实际响应调整
                        window.location.href = '/userhome?id=' + userId;
                    },
                    error: function() {
                        alert('登录失败，请检查您的凭据');
                    }
                });
            });
        });
    </script>
</div>
</body>
</html>